package com.huiyujinrong.qiangdanbao.model;


import com.huiyujinrong.qiangdanbao.bean.FaceEngineBean;
import com.huiyujinrong.qiangdanbao.bean.FaceFindBean;
import com.huiyujinrong.qiangdanbao.bean.UserInforBean;
import com.huiyujinrong.qiangdanbao.bean.UserUploadBean;
import com.huiyujinrong.qiangdanbao.network.AppService;
import com.huiyujinrong.qiangdanbao.utils.RequestInterceptor;

import java.io.File;

import javax.inject.Inject;

import happyloan.core.EventBusUtil;
import happyloan.core.RespBase;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;

public class UserNoInforModel {

    private AppService service;
    private MultipartBody.Part WORK_GP = null;
    private MultipartBody.Part WORK_MP = null;

    @Inject
    UserNoInforModel(AppService s) {
        this.service = s;
    }

    public void uploadUserInformation(String user_id, String true_name, String idcard_no,
                                      String organize_name, String ad_code, String ad_path,
                                      File id_just, File id_back, File work_gongpai,
                                      File work_mingpian) {

        MultipartBody.Part ID_JUST = MultipartBody.Part.createFormData("idcard_front_file", "",
                RequestBody.create(MediaType.parse("image/*"), id_just));

        MultipartBody.Part ID_BACK = MultipartBody.Part.createFormData("idcard_back_file", "",
                RequestBody.create(MediaType.parse("image/*"), id_back));


        if (work_gongpai != null && work_mingpian != null) {
            WORK_GP = MultipartBody.Part.createFormData("work_card_file", "",
                    RequestBody.create(MediaType.parse("image/*"), work_gongpai));
            WORK_MP = MultipartBody.Part.createFormData("business_card_file", "",
                    RequestBody.create(MediaType.parse("image/*"), work_mingpian));
        } else if (work_gongpai != null) {
            WORK_GP = MultipartBody.Part.createFormData("work_card_file", "",
                    RequestBody.create(MediaType.parse("image/*"), work_gongpai));
        } else if (work_mingpian != null) {
            WORK_MP = MultipartBody.Part.createFormData("business_card_file", "",
                    RequestBody.create(MediaType.parse("image/*"), work_mingpian));
        } else {
            return;
        }


        RequestBody userID1 = RequestBody.create(MediaType.parse("text/plain"), user_id);
        RequestBody userID2 = RequestBody.create(MediaType.parse("text/plain"), true_name);
        RequestBody userID3 = RequestBody.create(MediaType.parse("text/plain"), idcard_no);
        RequestBody userID4 = RequestBody.create(MediaType.parse("text/plain"), organize_name);
        RequestBody userID5 = RequestBody.create(MediaType.parse("text/plain"), ad_code);
        RequestBody userID6 = RequestBody.create(MediaType.parse("text/plain"), ad_path);

        RequestInterceptor.outSign = true;
        RequestInterceptor.extraMap.put("user_id", user_id);
        RequestInterceptor.extraMap.put("true_name", true_name);
        RequestInterceptor.extraMap.put("idcard_no", idcard_no);
        RequestInterceptor.extraMap.put("organize_name", organize_name);
        RequestInterceptor.extraMap.put("ad_code", ad_code);
        RequestInterceptor.extraMap.put("ad_path", ad_path);
        Call<RespBase<UserUploadBean>> call = service.uploadUinfor(userID1, userID2, userID3, userID4, userID5, userID6, ID_JUST, ID_BACK, WORK_GP, WORK_MP);

        EventBusUtil<UserUploadBean> util = new EventBusUtil<>();
        util.sendEvent4Call(call);
    }

    public void getUserInforDta(String user_id) {
        EventBusUtil<UserInforBean> util = new EventBusUtil<>();
        util.sendEvent4Call(service.selectUserInfor(user_id));
    }

    public void findZmxyFaceRecordByUserId(String user_id) {
        EventBusUtil<FaceFindBean> busUtil = new EventBusUtil<>();
        busUtil.sendEvent4Call(service.findZmxyFaceRecordByUserId(user_id));
    }

    /**
     * 人脸识别
     *
     * @param user_id
     */
    public void getZmxyFaceEngine(String user_id) {
        EventBusUtil<FaceEngineBean> busUtil = new EventBusUtil<>();
        busUtil.sendEvent4Call(service.getZmxyFaceEngine(user_id));
    }
}
